package com.feeyo.groundservice.composewidget.usage

import Banner
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import com.feeyo.groundservice.composewidget.usage.base.BaseComposeActivity
import com.feeyo.groundservice.composewidget.view.banner.rememberBannerState
import com.feeyo.groundservice.composewidget.view.image.CustomAsyncImage
import com.feeyo.groundservice.composewidget.view.pager_indicator.PagerIndicator


/**
 *作者：QLH on 2023-08-05
 *描述：轮播图
 */
class BannerActivity : BaseComposeActivity() {

    private val images = mutableStateListOf(
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F66b7ce397068c1f4710cafe4e1827ab5f7565180.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665065304&t=c45a94bbac62a3bd502dc53e40afc583",
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2Fc0793b2877f09ded49e96e3b3e05781d4f1e2e9e.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665065303&t=86940ecb5bf20d1c90b5fb1c1f0afb06",
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F79c593c97cb1aef62160a7c6165ea3ecdc60f064.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665065301&t=0d840bcfe779bcac4ea65db6d257c417",
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F2726b76584c11dc75449024ad6105893be1edd0f.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1665065300&t=7afae3a8eccb498fb37d54ff54d2016b",
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            Column {
                horizontalBanner(Modifier.fillMaxWidth().weight(1f))
                verticalBanner(Modifier.fillMaxWidth().weight(1f))
            }
        }
    }

    @Composable
    private fun verticalBanner(modifier: Modifier) {
        val bannerState = rememberBannerState()
        Box(modifier = modifier) {
            Banner(
                pageCount = images.size,
                modifier = Modifier.fillMaxSize(),
                bannerState = bannerState,
                orientation = Orientation.Vertical,
            ) {
                CustomAsyncImage(
                    model = images[index],
                    contentDescription = null,
                    contentScale = ContentScale.FillBounds
                )
            }
            //指示器
            PagerIndicator(
                size = images.size,
                offsetPercentWithSelectFlow = remember { bannerState.createChildOffsetPercentFlow() },
                selectIndexFlow = remember { bannerState.createCurrSelectIndexFlow() },
                indicatorItem = {
                    Spacer(modifier = Modifier.size(10.dp).background(Color.Gray, CircleShape))
                },
                selectIndicatorItem = {
                    Spacer(modifier = Modifier.size(10.dp).background(Color.Black, CircleShape))
                },
                modifier = Modifier.align(Alignment.CenterEnd).padding(10.dp),
                orientation = Orientation.Vertical
            )
        }
    }

    @Composable
    private fun horizontalBanner(modifier: Modifier) {
        val bannerState = rememberBannerState()
        Box(modifier = modifier){
            Banner(
                pageCount = images.size,
                modifier = Modifier.fillMaxSize(),
                bannerState = bannerState,
                autoScroll = false,
                orientation = Orientation.Horizontal,
            ) {
                Log.e("111","index:$index---${bannerState.getCurrSelectIndex()}")
                CustomAsyncImage(
                    model = images[index],
                    contentDescription = null,
                    contentScale = ContentScale.FillBounds
                )
            }

            //指示器
            PagerIndicator(
                size = images.size,
                offsetPercentWithSelectFlow = remember { bannerState.createChildOffsetPercentFlow() },
                selectIndexFlow = remember { bannerState.createCurrSelectIndexFlow() },
                indicatorItem = {
                    Spacer(modifier = Modifier.size(10.dp).background(Color.Gray, CircleShape))
                },
                selectIndicatorItem = {
                    Spacer(modifier = Modifier.size(15.dp,10.dp).background(Color.Black, CircleShape))
                },
                modifier = Modifier.align(Alignment.BottomCenter).padding(10.dp),
                orientation = Orientation.Horizontal
            )
        }

    }


}